﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Microsoft.Live
{
    public static class AuthClientExtensions
    {
        public static Task<LiveConnectSession> InitializeAsyncTask(this LiveAuthClient authClient,
            IEnumerable<string> scopes = null, object userstate = null)
        {
            var tcs = new TaskCompletionSource<LiveConnectSession>();

            try
            {
                authClient.InitializeCompleted += (s, e) =>
                                            {
                                                if (e.Cancelled)
                                                {
                                                    tcs.TrySetCanceled();
                                                }
                                                else if (e.Session != null && e.Status == LiveConnectSessionStatus.Connected)
                                                {
                                                    tcs.TrySetResult(e.Session);
                                                }
                                                else
                                                {
                                                    tcs.TrySetResult(null);
                                                }
                                            };

                if (scopes != null && userstate != null)
                {
                    authClient.InitializeAsync(scopes, userstate);
                }
                else if (scopes != null)
                {
                    authClient.InitializeAsync(scopes);
                }
                else if (userstate != null)
                {
                    authClient.InitializeAsync(userstate);
                }
                else
                {
                    authClient.InitializeAsync();
                }
            }
            catch (Exception ex)
            {
                tcs.TrySetException(ex);
            }

            if (tcs.Task.Exception != null)
            {
                throw tcs.Task.Exception;
            }

            return tcs.Task;
        }

        public static Task<LiveConnectSession> LoginAsyncTask(this LiveAuthClient authClient,
            IEnumerable<string> scopes, object userstate = null)
        {
            var tcs = new TaskCompletionSource<LiveConnectSession>();

            try
            {
                authClient.LoginCompleted += (s, e) =>
                {
                    if (e.Session != null && e.Status == LiveConnectSessionStatus.Connected)
                    {
                        tcs.TrySetResult(e.Session);
                    }
                    else
                    {
                        tcs.TrySetResult(null);
                    }
                };


                if (userstate != null)
                {
                    authClient.LoginAsync(scopes, userstate);
                }
                else
                {
                    authClient.LoginAsync(scopes);
                }
            }
            catch (Exception ex)
            {
                tcs.TrySetException(ex);
            }

            if (tcs.Task.Exception != null)
            {
                throw tcs.Task.Exception;
            }

            return tcs.Task;
        }
    }
}
